package com.bw.launcher.splash

import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.baseapplication.BaseApplication
import com.bw.common.mvi.BaseActivity
import com.bw.launcher.FrameActivity
import com.bw.launcher.databinding.ActivitySplashBinding
import com.bw.widget.navigation.AppConfig
import io.virtualapp.hook_sdk.firewall.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class SplashActivity : BaseActivity<ActivitySplashBinding,SplashViewModel>(){
    lateinit var hookSDK : HookSDK
    override fun createVM(): SplashViewModel {
        return viewModels<SplashViewModel>().value
    }

    override fun initView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            window.decorView.systemUiVisibility = (
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
        }
        BarUtils.addMarginTopEqualStatusBarHeight(v.root)
        BarUtils.setStatusBarColor(this, Color.WHITE)
        ARouter.init(BaseApplication.baseApplication)
        ARouter.openLog()
        ARouter.debuggable()
    }

    override fun initData() {
        vm.requestWhite()//请求白名单
    }

    override fun bindState() {
        lifecycleScope.launch {
            vm.mutableContainer.uiStateFlow.collect{
                if (it.lockState){
                    ToastUtils.showShort("设备已锁请联系管理员解锁")
                    finish()
                }else if (it.whiteState) {
                    //白名单
                    var array = ArrayList<AppNetInfo>()
                    it.datas.forEach {
                        val info = AppNetInfo("",it.url,"")
                        array.add(info)
                    }
                    //防火墙技术
//                    FireWallConfig.array = array //白名单
//                    FireWallConfig.type = FireWallType.WHITE //类型：白名单
//                    val build = HookSDK.Build()
//                    build.configHookType(HookType.All) //hook类型：NONE,WEB,HTTP,All;
////                    FireWallConfig.whiteStr = AppConfig().parseAssets("white.xml")
////                    build.init(BaseApplication.baseApplication,"com.bw.mdm")
//                    hookSDK = build.build()
//                    hookSDK.hook()
                    GlobalScope.launch {
                        val timmer = async { timmer() }
                        timmer.await()
                        startActivity(Intent(this@SplashActivity, FrameActivity::class.java))
                        this@SplashActivity.finish()
                    }
                }
            }
        }
    }

    override fun createViewBinding(): ViewBinding {
        return ActivitySplashBinding.inflate(LayoutInflater.from(this))
    }

    private suspend fun timmer(){
        delay(3*1000)
    }

}